package pl.rzarajczyk.utils.system.notification;

import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.apache.commons.logging.Log;
import pl.rzarajczyk.breaktime.utils.SwingUtils;
import pl.rzarajczyk.utils.log.LazyLogFactory;

class NotificationJFrame extends javax.swing.JFrame {
    
    public static final int TIMEOUT = 5000;
    
    private Log log = LazyLogFactory.getLog(getClass());
    private Timer timer;
    
    public NotificationJFrame(String title, String text, File iconFile) {
        initComponents();
        Shape shape = new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(), 20, 20);
        AWTUtilities.setWindowShape(this, shape);
        setOpacity(0.85f);
        this.getContentPane().setBackground(new Color(21, 21, 21));
        this.setLocation(SwingUtils.getScreenWidth() - this.getWidth() - 20, 100);
        this.title.setText(title);
        this.label.setText(text);
        try {
            Icon icon = new ImageIcon(ImageIO.read(iconFile));
            this.label.setIcon(icon);
        } catch (IOException e) {
            log.warn("Unable to read icon image", e);
        }
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                NotificationJFrame.this.setVisible(false);
                NotificationJFrame.this.dispose();
            }
            
        };
        timer = new Timer();
        timer.schedule(task, TIMEOUT);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label = new javax.swing.JLabel();
        title = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setUndecorated(true);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        label.setForeground(new java.awt.Color(255, 255, 255));
        label.setText("test");
        label.setPreferredSize(new java.awt.Dimension(25, 32));

        title.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        title.setForeground(new java.awt.Color(255, 255, 255));
        title.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addComponent(title))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        timer.cancel();
        setVisible(false);
        dispose();
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel label;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables
}
